#ifndef __STORMWIDGET_CONFIGMANAGE_UTL_STRNUM_UTL_STRNUM_H
#define __STORMWIDGET_CONFIGMANAGE_UTL_STRNUM_UTL_STRNUM_H

#include "stdafx.h"
#include <string.h>
#include <locale.h>
#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")


const int STRING_MAX_LENGTH  =  8192;

inline BYTE CharToBin(TCHAR ch)
{
	if(ch>='0' && ch<='9')
		return ch - '0';

	if(ch>='a' && ch<='f')
		return ch - 'a' + 10;

	if(ch>='A' && ch<='F')
		return ch - 'A' + 10;
	return 0xff;
}
inline bool Hex2Integer(LPCSTR lpHex, int nHexLen, unsigned int* pOut)
{
	UINT uNum = 0;
	for (int i = 0; i < nHexLen && i < 8; i++)
	{
		if (lpHex[i] == 0)
			break;

		BYTE nHalfByte = CharToBin(lpHex[i]);
		if (nHalfByte == 0xFF)
			return false;

		uNum = (uNum<<4)|nHalfByte;

	}

	*pOut = uNum;
	return true;
}

inline bool AnsiToUnicode(const char* pSz,LPTSTR lpsz)
{
	int BufSize = MultiByteToWideChar(936,0,pSz,-1,NULL,0); 
	TCHAR *pWszSource = new TCHAR[BufSize + 1];
	MultiByteToWideChar(936,0,pSz,-1,pWszSource,BufSize); 
	pWszSource[BufSize] = '\0';
	if(BufSize + 1 >= STRING_MAX_LENGTH)
		StrCpyN(lpsz,pWszSource,STRING_MAX_LENGTH);
	else
		StrCpyN(lpsz,pWszSource,BufSize);
	delete pWszSource;
	pWszSource = NULL;
	return true;
}

//UNICODEתANSI
inline bool  UnicodeToAnsi(LPCTSTR lpsz,char* pSz)
{
	int BufSize = WideCharToMultiByte(CP_ACP,NULL,lpsz,wcslen(lpsz),NULL,0,NULL,NULL);
	char* szAnsi = new char[BufSize + 1];
	WideCharToMultiByte(CP_ACP,NULL,lpsz,wcslen(lpsz),szAnsi,BufSize,NULL,NULL);
	szAnsi[BufSize] = '\0';
	if(BufSize + 1 >= STRING_MAX_LENGTH)
		strcpy_s(pSz,STRING_MAX_LENGTH - 1,szAnsi);
	else
		strcpy_s(pSz,BufSize + 1,szAnsi);
	delete szAnsi;
	szAnsi = NULL;

	return true;
}

#endif //__STORMWIDGET_CONFIGMANAGE_UTL_STRNUM_UTL_STRNUM_H